<?php $this->placeholder('container-class')->set('post_ad')?>
<?php $this->JQuery()->javascriptCaptureStart()?>


    // Control for Title
    $('input#title').keydown(function(event) {
        var charLength = 100 - $(this).val().length;
        if (charLength <0) {
            if ( event.keyCode == 8 || event.keyCode == 46 ) {

            } else {
                return false;
            }
        }
        else {
            $('span#counter_title').html('<em>'+charLength + '</em> <?php echo $this->t('characters remaining')?>');
        }
    });

    // Control for Description
    $('textarea#description').keydown(function(event) {
        var charLength = 3000 - $(this).val().length;
        if (charLength <0) {
            if ( event.keyCode == 8 || event.keyCode == 46 ) {

            } else {
                return false;
            }
        }
        else {
            $('span#counter_description').html('<em>'+charLength + '</em> <?php echo $this->t('characters remaining')?>');
        }
    });

    $j('#preview_link').click(function() {
        var action = $j('#listing_form').attr('action');
        $j('#listing_form').attr('action', '<?php echo $this->preview_link?>');
        $j('#listing_form').attr('target','_blank');
        $j('#listing_form').submit();
        $j('#listing_form').attr('action', action);
        $j('#listing_form').attr('target','_self');
        return false;
    });

    //Online Only Area
    if ($j("#area_l1").val() == "4cbe455a8835c17c10060000") {
        $j("#district_div").hide();
    }
    else {
        $j("#district_div").show();
    }

<?php $this->JQuery()->javascriptCaptureEnd()?>

<form action="<?php echo $this->escape($this->form->getAction()) ?>" method="<?php echo $this->escape($this->form->getMethod())?>" id="listing_form"  enctype="multipart/form-data">
    <fieldset>
        <legend><?php echo $this->t('Create Your Ad');?></legend>
        <div class="required"><?php echo $this->t('Required Information')?></div>
        <p class="category desc">
          <label><?php echo $this->t('Selected Categories')?> 
                (<a href="<?php echo $this->geturl('post-an-ad', 'listing', null, null, array('category' => $_GET['category'], 'change' => 1))?>">Change</a>)
              :</label>
        </p>
          <ul>
              <?php foreach($this->category as $cat):?>
                  <li>
                      <?php 
                        $tmp = array();
                        foreach($cat->getParentTree() as $p) {
                            $tmp[] = $p->name;
                        }
                        echo implode(' &gt; ', $tmp);
                      ?>
                  </li>
              <?php endforeach;?>
         </ul>
        
        <input type="hidden" name="adid" id="adid" value="<?php echo $this->adid?>">
        <?php foreach($this->category as $cat):?>
            <input type="hidden" name="category[]" value="<?php echo $cat->_id?>">
        <?php endforeach;?>
            
        <div class="title text">
          <label class="required"><?php echo $this->t('Title')?></label>
          <span id="counter_title"><?php echo $this->t('<em>100</em> characters remaining')?></span>
          <?php echo $this->form->title ?>
          <?php if (!empty($_POST['title'])): ?>
              <?php if (preg_match("#.[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}.#", $_POST['title'])): ?>
                  <ul class="errors"><li><?php echo $this->t('Please remove email')?></li></ul>
              <?php endif;?>
          <?php endif; ?>
        </div>
        <div class="description textarea">
          <label class="required"><?php echo $this->t('Description')?></label>
          <span id="counter_description"><?php echo $this->t('<em>3000</em> characters remaining')?></span>
          <?php echo $this->form->description ?>
          <?php if (!empty($_POST['description'])): ?>
              <?php if (preg_match("#.[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}.#", $_POST['description'])): ?>
                  <ul class="errors"><li><?php echo $this->t('Please remove email')?></li></ul>
              <?php endif;?>
          <?php endif; ?>

          <span id="info_description"><?php echo $this->t('To prevent spam, do not enter any email addresses.')?></span>
        </div>        
        <div class="price radio text">
            <span class="required"><?php echo $this->t('Price')?></span>
            <?php foreach ($this->form->price->options as $k => $opt):?>
                <input type="radio" name="price" id="price-<?php echo $k?>" value="<?php echo $k?>" <?php echo $this->form->price->getValue() == $k ? 'checked' : ''?>>
                <?php if($k==1):?>
                    <label><?php echo $opt?></label>
                    <label><input type="text" name="price_text_value" value="<?php echo $this->form->price_text_value->getValue()?>" id="price_text_value"></label>                    
                <?php else:?>
                    <label><?php echo $opt?></label>
                <?php endif;?>
            <?php endforeach;?>
        </div>
        <ul class="errors">
            <?php if (isset($_POST)):?>
                <?php if ((isset($_POST['price_text_value'])) && ($_POST['price'] == 1)):?>
                    <?php if (empty($_POST['price_text_value'])):?>
                        <li><?php echo $this->t('This information is required')?></li>
                    <?php elseif (!is_numeric($_POST['price_text_value'])):?>
                        <li><?php echo $this->t('Numbers Only')?></li>
                    <?php endif;?>
                <?php endif;?>
            <?php endif;?>
        </ul>
        <div class="location select">
            <div class="area select">
              <label class="required"><?php echo $this->t('Area')?></label>
              <?php echo $this->form->area_l1 ?>
            </div>
            <div class="district select" id="district_div">
              <label class="required"><?php echo $this->t('District')?></label>
              <?php echo $this->form->area_l2 ?>
            </div>
        </div>
        <div class="description textarea">
          <label><?php echo $this->t('Street Address')?></label>
          <?php echo $this->form->street ?>
        </div>
            
        <?php echo $this->Partial('/index/photo-uploader.phtml', 'listing', array('listing' => $this->listing));?>

        <p class="button_em">

          <button type="submit"><?php echo $this->t('Post Your Ad')?></button>
        </p>
        <p class="preview button">
            <button id="preview_link"><?php echo $this->t('Preview Your Ad')?></button>
        </p>
        <p><?php //echo $this->t('Please complete the field(s) marked in red')?></p>
    </fieldset>
</form>
